<template>
	<div class="system-role-dialog-container">
		<el-dialog :title="state.dialog.title" @close="closeDialog" v-model="state.dialog.isShowDialog" width="769px">
			<el-form ref="menuDialogFormRef" :model="ruleForm" size="default" label-width="120px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="道路编号：" prop="number" :rules="rules.number">
							<el-input :disabled="disabledFlag" size="default"  placeholder="道路编号" style="max-width: 400px" v-model="ruleForm.number"> </el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="道路名称：" prop="name" :rules="rules.name">
							<el-input size="default" placeholder="道路名称" style="max-width: 400px" v-model="ruleForm.name"> </el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item filterable label="所属区域：" prop="region_id" :rules="rules.region_id">
							<el-select clearable  placeholder="所属区域" style="width: 400px;" v-model="ruleForm.region_id">
								<el-option v-for="item in selectData" :key="item.id" :label="item.name" :value="item.id"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="道路等级：" prop="level" :rules="rules.level">
							<el-select clearable v-model="ruleForm.level"  placeholder="道路等级" style="width: 400px;">
								<el-option label="快速路" :value="1"></el-option>
								<el-option label="主干道" :value="2"></el-option>
								<el-option label="次干道" :value="3"></el-option>
								<el-option label="支路" :value="4"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="启用状态：" prop="status" :rules="rules.status">
							<el-select clearable v-model="ruleForm.status"  placeholder="启用状态" style="width: 400px;">
								<el-option label="停用" :value="0"></el-option>
								<el-option label="启用" :value="1"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="systemRoleDialog">
import { onMounted, reactive, ref ,defineProps,watch} from 'vue';
import {  ElMessage, type FormRules } from 'element-plus'
import {useRoleApi} from "/@/api/system/role/index"
import {useBasicApi} from "/@/api/system/menu/index"
// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);
const RoleApi = useRoleApi();
const BasicApi = useBasicApi()
const props = defineProps(['tableData','disabled'])
// dx定义start
	interface RuleForm {
		number:string,
		name:string,
		status:any,
		level:any,
		region_id:any
	}
	const ruleForm = ref<RuleForm>({
		number:"",
		name:"",
		status:"",
		level:"",
		region_id:""
	})

	let searchForm = ref({})
	const disabledFlag = ref(false)
	const selectData = ref(null)

	const rules = reactive<FormRules<RuleForm>>({
		number:[
			{ required: true, message: '道路编号必填', trigger: 'blur' },
		],
		name:[
			{ required: true, message: '道路名称必填', trigger: 'blur' },
		],
		status:[
			{ required: true, message: '启用状态必填', trigger: 'blur' },
		],
		level:[
			{ required: true, message: '所属区域必填', trigger: 'blur' },
		],
		region_id:[
			{ required: true, message: '道路必填', trigger: 'blur' },
		]
	})

	const getSelectData = (async ()=>{
		searchForm.value.search = `status:1`
		let res = await BasicApi.BasicTableApi(searchForm.value);
		if(res.isSuccess){
			selectData.value = res.returnData
		}
	})

	// 监听父组件传递过来的数据变化
	watch(props,(newValue)=>{
		ruleForm.value = newValue.tableData
		disabledFlag.value = newValue.disabled
	})
// dx定义end
const menuDialogFormRef = ref(null)
const state = reactive({
	ruleForm: {
		roleName: '', // 角色名称
		roleSign: '', // 角色标识
		sort: 0, // 排序
		status: true, // 角色状态
		describe: '', // 角色描述
	},
	menuData: [] as TreeType[],
	menuProps: {
		children: 'children',
		label: 'label',
	},
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});

// 打开弹窗
const openDialog = () => {
	
		state.dialog.title = '新增道路';
		state.dialog.submitTxt = '新 增';
		// 清空表单，此项需加表单验证才能使用
		// nextTick(() => {
		// 	roleDialogFormRef.value.resetFields();
		// });
	
	state.dialog.isShowDialog = true;
	// getMenuData();
};
// 关闭弹窗
const closeDialog = () => {
	menuDialogFormRef.value.resetFields()
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = (async ()=>{
	// 添加
		let res = await RoleApi.RoleAddApi(ruleForm.value);
		if(res.isSuccess){
			ElMessage.success('添加成功')
			closeDialog();
			emit('refresh');
		}else{
			ElMessage.success(res.returnMsg)
		}
})

onMounted(()=>{
	getSelectData()
})


// 暴露变量
defineExpose({
	openDialog,
});
</script>

<style scoped lang="scss">
.system-role-dialog-container {
	.menu-data-tree {
		width: 100%;
		border: 1px solid var(--el-border-color);
		border-radius: var(--el-input-border-radius, var(--el-border-radius-base));
		padding: 5px;
	}
}
</style>
